<template>
  <div class="facility-maintenance-record-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>公共设施保养记录列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="公共设施名称" prop="facilityName">
        <el-input v-model="searchParams.facilityName" placeholder="请选择公共设施名称" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findFacility"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="维修保养人员" prop="staffName">
        <el-input v-model="searchParams.staffName" placeholder="请选择维修保养人员" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findStaff"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="facilityName" label="公共设施名称"></el-table-column>
      <el-table-column prop="maintenanceStartDate" label="保养开始时间"></el-table-column>
      <el-table-column prop="maintenanceEndDate" label="保养开始时间"></el-table-column>
      <el-table-column prop="staffName" label="维修保养人员"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="facilitySelectorVisible" v-if="facilitySelectorVisible" title="公共设施信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <facility-selector @facility-selected-event="handleFacilitySelectedEvent">
      </facility-selector>
    </el-dialog>
    <el-dialog :visible.sync="staffSelectorVisible" v-if="staffSelectorVisible" title="维修保养人员选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <staff-selector @staff-selected-event="handleStaffSelectedEvent">
      </staff-selector>
    </el-dialog>
    <el-dialog :visible.sync="facilityMaintenanceRecordAddVisible" v-if="facilityMaintenanceRecordAddVisible" :title="facilityMaintenanceRecordAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <facility-maintenance-record-add :id="selectedFacilityMaintenanceRecordId" @close-facility-maintenance-record-add-event="handleCloseFacilityMaintenanceRecordAddEvent" @reset-facility-maintenance-record-add-event="handleResetFacilityMaintenanceRecordAddEvent">
      </facility-maintenance-record-add>
    </el-dialog>
    <el-dialog :visible.sync="facilityMaintenanceRecordViewVisible" v-if="facilityMaintenanceRecordViewVisible" title="公共设施保养记录详情" :top="dialogTop" :width="dialogWidth">
      <facility-maintenance-record-view :id="selectedFacilityMaintenanceRecordId" @close-facility-maintenance-record-view-event="handleCloseFacilityMaintenanceRecordViewEvent">
      </facility-maintenance-record-view>
    </el-dialog>
  </div>
</template>

<script>
import facilityMaintenanceRecordApi from '@/api/facilityMaintenanceRecordApi'
import FacilitySelector from "@/views/facility/FacilitySelector.vue";
import StaffSelector from "@/views/staff/StaffSelector.vue";
import FacilityMaintenanceRecordAdd from "@/views/facilityMaintenanceRecord/FacilityMaintenanceRecordAdd.vue"
import FacilityMaintenanceRecordView from "@/views/facilityMaintenanceRecord/FacilityMaintenanceRecordView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'

export default {
  name: 'FacilityMaintenanceRecordList',
  components: {FacilitySelector,StaffSelector,FacilityMaintenanceRecordAdd, FacilityMaintenanceRecordView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        facilityId: '',
        facilityName: '',
        staffId: '',
        staffName: '',
      },
      facilitySelectorVisible: false,
      staffSelectorVisible: false,
      facilityMaintenanceRecordAddVisible: false,
      facilityMaintenanceRecordAddTitle: '',
      facilityMaintenanceRecordViewVisible: false,
      selectedFacilityMaintenanceRecordId: 0
    }
  },
  methods: {
    getPageData (p) {
      return facilityMaintenanceRecordApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        facilityId: '',
        facilityName: '',
        staffId: '',
        staffName: '',
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedFacilityMaintenanceRecordId = ''
      this.facilityMaintenanceRecordAddVisible = true
      this.facilityMaintenanceRecordAddTitle = '公共设施保养记录新增'
    },
    onExport () {
      const headers = ['公共设施名称','保养开始时间','保养开始时间','维修保养人员','备注']
      const params = Object.assign(this.getPaginationParams(), this.searchParams)
      this.getPageData(params).then(data => {
        if (!data || !data.data || data.data.list.length < 1) {
          this.$message.error('无数据导出')
          return
        }
        const exportData = []
        for (const d of data.data.list) {
          exportData.push([d.facilityName,d.maintenanceStartDate,d.maintenanceEndDate,d.staffName,d.remark])
        }
        this.exportToExcel(headers, exportData)
      })
    },
    editRow (id) {
      this.selectedFacilityMaintenanceRecordId = id
      this.facilityMaintenanceRecordAddVisible = true
      this.facilityMaintenanceRecordAddTitle = '公共设施保养记录编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        facilityMaintenanceRecordApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedFacilityMaintenanceRecordId = id
      this.facilityMaintenanceRecordViewVisible = true
    },
    findFacility () {
      this.facilitySelectorVisible = true
    },
    handleFacilitySelectedEvent (selectedFacility) {
      this.facilitySelectorVisible = false
      if (selectedFacility && 'facilityId' in selectedFacility && this.searchParams.facilityId !== selectedFacility['facilityId']) {
        this.searchParams.facilityId = selectedFacility['facilityId']
        this.searchParams.facilityName = selectedFacility['facilityName']
      }
    },
    findStaff () {
      this.staffSelectorVisible = true
    },
    handleStaffSelectedEvent (selectedStaff) {
      this.staffSelectorVisible = false
      if (selectedStaff && 'staffId' in selectedStaff && this.searchParams.staffId !== selectedStaff['staffId']) {
        this.searchParams.staffId = selectedStaff['staffId']
        this.searchParams.staffName = selectedStaff['staffName']
      }
    },
    handleCloseFacilityMaintenanceRecordViewEvent () {
      this.facilityMaintenanceRecordViewVisible = false
    },
    handleResetFacilityMaintenanceRecordAddEvent () {
      this.facilityMaintenanceRecordAddTitle = '公共设施保养记录新增'
    },
    handleCloseFacilityMaintenanceRecordAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.facilityMaintenanceRecordAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>